package com.sqstudio.view
{
	import com.sqstudio.common.DataProxy;
	import com.sqstudio.common.LunarCalendar;
	import com.sqstudio.model.Model;
	import com.sqstudio.ui.GridMC;
	import com.sqstudio.vo.ItemVo;
	
	import flash.display.Sprite;
	import flash.events.DataEvent;
	
	public class GridPanel extends Sprite
	{
		private var gridSp:Sprite;
		public function GridPanel(d:Date){
			var gridMC:GridMC = new GridMC();
			addChild(gridMC);
			this.gridSp = new Sprite();
			this.addChild(this.gridSp);
			this.gridSp.y = 29;
		}
		
		
		/**
		 *刷新表格日期 
		 * @param d
		 * 
		 */		
		public function updateGrid(d:Date):void{
			setCalendar(d);
		}
		
		
		private function setCalendar(d:Date):void{
			while(this.gridSp.numChildren>0){
				this.gridSp.removeChildAt(0);
			}
			var tmpDate:Date = new Date(d.getFullYear(),d.getMonth(),d.getDate());
			var lc:LunarCalendar = new LunarCalendar(tmpDate);
			var days:int = lc.solarDays(tmpDate.getFullYear(),tmpDate.month);
			tmpDate.date=1;
			var len:int = days + tmpDate.day > 35?42:35;
			tmpDate.date -= tmpDate.day;
			
			var itemW:int =385/7 ;
			var itemH:Number = len==35?44:36.5;
			
			for(var i:int=0;i<len/7;i++){
				for(var j:int=0;j<7;j++){
					var itemVo:ItemVo = new ItemVo();
					itemVo.isBig = (len==35);
					var itemDate:Date =  DataProxy.getDateCopy(tmpDate);
					itemVo.lc = new LunarCalendar(itemDate);
					itemVo.date = itemDate;
					itemVo.isCrtDay = DataProxy.equalsDate(itemVo.date,d);
					itemVo.isCrtMonth = itemDate.getMonth() == Model.CrtDate.getMonth();
					itemVo.isHoliday = DataProxy.checkIsHoliday(itemVo.date);
					itemVo.isWork = DataProxy.checkIsWork(itemVo.date);
					var gridItem:GridItem = new GridItem(itemVo);
					this.gridSp.addChild(gridItem);
					gridItem.x = j * itemW;
					gridItem.y = i * itemH;
					tmpDate.date++;
				}
			}
		}
		
	}
}